package com.share.demo.service;

import com.share.demo.domain.Page;
import com.share.demo.mapper.BaseMapper;
import com.share.demo.domain.BaseModel;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * caifan 2017/9/10
 */
public abstract class BaseService<T>  {
    @Autowired
    BaseMapper<T> mapper;

    public BaseModel findById (Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public int insert(BaseModel baseModel) {
        baseModel.setCreateTime(new Date());
        baseModel.setUpdateTime(new Date());
        return mapper.insert(baseModel);
    }

    public int update(BaseModel baseModel) {
        baseModel.setUpdateTime(new Date());
        return mapper.updateByPrimaryKey(baseModel);
    }

    public List<BaseModel> select() {
        return mapper.selectAll();
    }

    public BaseModel selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<BaseModel> selectPage(BaseModel baseModel, Page page) {
        return mapper.selectByRowBounds(baseModel, new RowBounds(page.getOffset(), page.getLimit()));
    }

    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }
}
